<template>
  <div class="body" @mousedown="mousedown">
    <div class="container">
      <!-- 登录或者注册表单 -->
      <div class="form-box">
        <!-- 注册表单 -->
        <div class="register-box" :class="{ hidden: !showRegister }">
          <h1>register</h1>
          <el-form :model="registerForm" label-width="100px">
            <el-form-item label="用户名">
              <el-input v-model="registerForm.username" placeholder="用户名"></el-input>
            </el-form-item>
            <el-form-item label="密码">
              <el-input
                type="password"
                v-model="registerForm.password"
                placeholder="密码"
                show-password
              ></el-input>
            </el-form-item>
            <el-form-item label="确认密码">
              <el-input
                type="password"
                v-model="registerForm.confirmPassword"
                placeholder="确认密码"
                show-password
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleRegister">注册</el-button>
            </el-form-item>
          </el-form>
        </div>
        <!-- 登录表单 -->
        <div class="login-box" :class="{ hidden: showRegister }">
          <h1>login</h1>
          <el-form :model="loginForm" label-width="100px">
            <el-form-item label="用户名">
              <el-input v-model="loginForm.username" placeholder="用户名"></el-input>
            </el-form-item>
            <el-form-item label="密码">
              <el-input
                type="password"
                v-model="loginForm.password"
                placeholder="密码"
                show-password
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleLogin">登录</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>

      <!-- 左侧登录按钮和图例 -->
      <div class="con-box left">
        <h2>欢迎来到<span>学霸打卡</span></h2>
        <img src="./images/1.png" alt="" />
        <p>已有账号</p>
        <button @click="toggleForm(false)">去登录</button>
      </div>

      <!-- 右侧注册按钮和图例 -->
      <div class="con-box right">
        <h2>欢迎来到<span>学霸打卡</span></h2>
        <img src="./images/2.png" alt="" />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { useUserInfo } from '@renderer/stores/userInfo'
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'

const showRegister = ref(false)
const registerForm = reactive({
  username: '',
  password: '',
  confirmPassword: ''
})
const loginForm = reactive({
  username: '',
  password: ''
})

/** 注册和登录按钮的切换 */
function toggleForm(isRegister: boolean) {
  showRegister.value = isRegister
  const form_box = document.querySelector('.form-box') as HTMLElement | null
  if (isRegister) {
    form_box!.style.transform = 'translateX(80%)'
  } else {
    form_box!.style.transform = 'translateX(0)'
  }
}
const router = useRouter()

const handleRegister = () => {
  // 注册逻辑
}

const handleLogin = async () => {
  // 调用登录方法，获取token信息
  await useUserInfo().login(loginForm)

  // 查询用户信息
  await useUserInfo().setUserInfos()

  //跳转路由
  router.push('/home')
}

const isKeyDown = ref(false)
const dinatesX = ref(0)
const dinatesY = ref(0)

const mousedown = (e) => {
  console.log('2222')
  isKeyDown.value = true
  dinatesX.value = e.x
  dinatesY.value = e.y

  document.onmousemove = (ev) => {
    if (isKeyDown.value) {
      const x = ev.screenX - dinatesX.value
      const y = ev.screenY - dinatesY.value
      //给主进程传入坐标
      const data = {
        appX: x,
        appY: y
      }
      electron.ipcRenderer.invoke('custom-adsorption', data)
    }
  }
  document.onmouseup = (ev) => {
    isKeyDown.value = false
  }
}
</script>

<style scoped>
/* 将原有的CSS样式粘贴到这里 */
* {
  margin: 0;
  padding: 0;
}

.body {
  height: 100vh;
  width: 100vw;
  display: flex;
  justify-content: center;
  align-items: center;
  /* background: url('./images/background.png'); */
  background-image: linear-gradient(
    to right top,
    #6b7ad1,
    #6e83d6,
    #728cda,
    #7794de,
    #7d9de2,
    #75a9eb,
    #6fb4f2,
    #69bff8,
    #52cffe,
    #41dfff,
    #46eefa,
    #5ffbf1
  );
  /* background-size: cover; */
  /* background-position: center; */
  /* background-repeat: no-repeat; */
}

.container {
  background-color: #fff;
  width: 650px;
  height: 415px;
  border-radius: 5px;
  box-shadow: 5px 5px 5px rgba(0, 0, 0, 0.1);
  justify-content: center;
  position: relative;
  /* 居中 */
  margin: auto;
  margin-top: 10%;
}

.form-box {
  position: absolute;
  top: -10%;
  left: 5%;
  /* background-color: #d3b7d8; */
  background-image: linear-gradient(
    to right top,
    #6b7ad1,
    #6e83d6,
    #728cda,
    #7794de,
    #7d9de2,
    #75a9eb,
    #6fb4f2,
    #69bff8,
    #52cffe,
    #41dfff,
    #46eefa,
    #5ffbf1
  );
  width: 320px;
  height: 500px;
  border-radius: 5px;
  box-shadow: 2px 0 10px rgba(0, 0, 0, 0.1);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 2;
  transition: 0.5s ease-in-out;
}

.register-box,
.login-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
}

.hidden {
  display: none;
  transition: 0.5s;
}

h1 {
  text-align: center;
  margin-bottom: 25px;
  text-transform: uppercase;
  color: #fff;
  letter-spacing: 5px;
}

input {
  background-color: transparent;
  width: 70%;
  color: #fff;
  border: none;
  border-bottom: 1px solid rgba(255, 255, 255, 0.4);
  padding: 10px 0;
  text-indent: 10px;
  margin: 8px 0;
  font-size: 14px;
  letter-spacing: 2px;
}

input::placeholder {
  color: #fff;
}

input:focus {
  color: #a262ad;
  outline: none;
  border-bottom: 1px solid #a262ad80;
  transition: 0.5s;
}

input:focus::placeholder {
  opacity: 0;
}

.form-box button {
  width: 70%;
  margin-top: 35px;
  background-color: #f6f6f6;
  outline: none;
  border-radius: 8px;
  padding: 13px;
  color: #a262ad;
  letter-spacing: 2px;
  border: none;
  cursor: pointer;
}

.form-box button:hover {
  background-color: #a262ad;
  color: #f6f6f6;
  transition: background-color 0.5s ease;
}

.con-box {
  width: 50%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
}

.con-box.left {
  left: -2%;
}

.con-box.right {
  right: -2%;
}

.con-box h2 {
  color: #8e9aaf;
  font-size: 25px;
  font-weight: bold;
  letter-spacing: 3px;
  text-align: center;
  margin-bottom: 4px;
}

.con-box p {
  font-size: 12px;
  letter-spacing: 2px;
  color: #8e9aaf;
  text-align: center;
}

.con-box span {
  color: #d3b7d8;
}

.con-box img {
  width: 150px;
  height: 150px;
  opacity: 0.9;
  margin: 40px 0;
}

.con-box button {
  margin-top: 3%;
  background-color: #fff;
  color: #a262ad;
  border: 1px solid #d3b7d8;
  padding: 6px 10px;
  border-radius: 5px;
  letter-spacing: 1px;
  outline: none;
  cursor: pointer;
}

.con-box button:hover {
  background-color: #d3b7d8;
  color: #fff;
}
</style>
